package com.example.demo.ReentrantLock锁;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @description:
 * @projectName:demo
 * @see:ReentrantLock锁
 * @author:袁成龙
 * @createTime:2020/10/7 13:12
 * @version:1.0
 */
public class TestRunnable extends  Thread{

    private ReentrantLock reentrantLock;

    public TestRunnable(ReentrantLock reentrantLock) {
        this.reentrantLock = reentrantLock;
    }

    @Override
    public void run() {
        //可以起到一个同步的作用,只要一个线程获取到了锁另一个就获取不到了,就会阻塞到这里,当获取到的那个执行完了阻塞的才会获取到锁接着执行
        reentrantLock.lock();
        try {
            for (int i = 0; i <10 ; i++) {
                try {
                    Thread.sleep(1000);
                    System.out.println("++++++"+Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            reentrantLock.unlock();
        }
    }
}